package com.xbongbong.saas.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.pojo.FieldAttrEntityForImport;
import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.dto.ImportFormDataDTO;
import com.xbongbong.paas.pojo.dto.SaasFormSaveDTO;
import com.xbongbong.paas.pojo.dto.ValidateDataDTO;
import com.xbongbong.paas.pojo.imports.CellTitlePojo;
import com.xbongbong.paas.pojo.imports.RowGroupPojo;
import com.xbongbong.paas.pojo.vo.FormDataAddGetVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.pro.businessproduct.pojo.dto.BusinessProductListDTO;
import com.xbongbong.pro.customer.pojo.dto.ChangeStatusDTO;
import com.xbongbong.pro.detailtab.vo.ProductSummaryVO;
import com.xbongbong.pro.formdata.pojo.AfterSavePojo;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainInLinkItemDTO;
import com.xbongbong.pro.instock.pojo.dto.RetMaterielListDTO;
import com.xbongbong.pro.instock.pojo.vo.RetMaterielListVO;
import com.xbongbong.pro.instockproduct.pojo.vo.InstockProductDeleteBatchVO;
import com.xbongbong.pro.listbatch.pojo.dto.AddLabelDTO;
import com.xbongbong.pro.product.pojo.ProductSavePojo;
import com.xbongbong.pro.product.pojo.dto.PagingProductDTO;
import com.xbongbong.pro.product.pojo.vo.PagingProductVO;
import com.xbongbong.pro.productionorder.pojo.dto.ProductionOrderDeleteBatchDTO;
import com.xbongbong.pro.productionorder.pojo.dto.ProductionOrderEditAttrUpdateDTO;
import com.xbongbong.pro.productionorder.pojo.dto.ProductionOrderListDTO;
import com.xbongbong.pro.productionorder.pojo.dto.ProductionOrderUpdateBatchDTO;
import com.xbongbong.pro.productionorder.pojo.dto.ProductionOrderUpdateByBusinessRuleDTO;
import com.xbongbong.pro.productionorder.pojo.vo.ProductionOrderDeleteBatchVO;
import com.xbongbong.pro.productionorder.pojo.vo.ProductionOrderListVO;
import com.xbongbong.pro.productionorder.pojo.vo.ProductionOrderUpdateBatchVO;
import com.xbongbong.pro.productionorderproduct.pojo.dto.FormatProductionOrderProductDTO;
import com.xbongbong.pro.simulationproduce.pojo.dto.ProductionOrderAddGetDTO;
import com.xbongbong.saas.domain.entity.OutstockProductEntity;
import com.xbongbong.saas.domain.entity.ProductionOrderProductEntity;
import com.xbongbong.saas.domain.entity.ext.ProductionOrderEntityExt;
import com.xbongbong.saas.enums.UpdateDataTypeEnum;

import java.util.List;
import java.util.Map;

/**
 * 生产单业务
 * @author feng.zheng
 * @version v1.0
 * @date 2019/1/18
 * @since v1.0
 */
public interface ProductionOrderService {
    /**
     * 生产单保存前saas特殊校验
     *
     * @param validateDataDTO saas特殊校验数据入参
     * @throws XbbException 业务异常
     * @author feng.zheng
     * @date 2019/1/18
     */
    void beforeSave(ValidateDataDTO validateDataDTO) throws XbbException;

    /**
     * 生产单保存后逻辑
     *
     * @param saasFormSaveDTO saas保存数据
     * @throws XbbException 业务异常
     * @author feng.zheng
     */
    AfterSavePojo afterSave(SaasFormSaveDTO saasFormSaveDTO) throws XbbException;

    /**
     * 生产单产品保存
     * @param productArray 产品数组
     * @param corpid 公司id
     * @param bomId 对应bom单的id
     * @param warehousrId 成品传入的是入库仓库id，物料传入的是出库仓库id
     * @author long.rao
     * @date 2019-02-01 15:19
     */
    void saveProduct(JSONArray productArray, String corpid, Long bomId, Long warehousrId) throws XbbException;

    /**
     * 新建时格式化解释
     *
     * @param formatExplainDTO 入参
     * @throws XbbException 异常
     * @author long.rao
     * @date 2019-02-22 14:55
     */
    void formatAddExplain(FormatExplainDTO formatExplainDTO) throws XbbException;



    /**
     * 格式化产品，显示给前端
     * @param dataList bom单实体数据
     * @param saasAnalysisDataDTO 数据解析DTO
     * @author long.rao
     * @date 2019-02-01 15:19
     */
//    void formatProduct4Show(JSONObject dataList, SaasAnalysisDataDTO saasAnalysisDataDTO) throws XbbException;

    /**
     * 查询生产单
     * @author 吴峰
     * @date 2019/3/1 11:01
     * @param productionOrderListDTO
     * @return com.xbongbong.pro.productionorder.pojo.vo.ProductionOrderListVO
     * @throws XbbException 业务异常
     */
    ProductionOrderListVO findProductionOrderList(ProductionOrderListDTO productionOrderListDTO) throws XbbException;

    /**
     * 格式化生产单
     * @author lcx
     * @date 2019/3/1 11:01
     * @param formatProductionOrderProductDTO
     * @throws XbbException 业务异常
     */
    JSONObject getFormatProductJsonArray(FormatProductionOrderProductDTO formatProductionOrderProductDTO) throws XbbException;

    /**
     * 获取生产单关联产品
     * @author lcx
     * @date 2019/3/1 11:01
     * @param businessProductListDTO
     * @param dataList 数据data实体
     * @throws XbbException 业务异常
     */
//    BusinessProductListVO getProductionOrderProductList(BusinessProductListDTO businessProductListDTO, JSONObject dataList) throws XbbException;

    /**
     * 获取物料出库产品的平均值
     * @param productionOrder 生产单实体
     * @author chaoxiong.lei
     * @Date 16:43 2018/8/29
     * @return map key为产品id，value为存有成本和数量属性的出库单产品实体
     **/
    Map<Long, OutstockProductEntity> getMaterielAvgCost(ProductionOrderEntityExt productionOrder);

    /**
     * 获取生产单物料可出库数量
     *
     * @param productionOrder   生产单实体
     * @param poProducts        生产单关联的物料产品
     * @author chaoxiong.lei
     * @Date 18:00 2018/10/19
     * @since v1.0
     * @return map key为产品id，value为可出库数量
     **/
    Map<Long, Double> getMaterielProductNum(ProductionOrderEntityExt productionOrder, List<ProductionOrderProductEntity> poProducts) throws XbbException;

    /**
     * 批量更新
     * @author 吴峰
     * @date 2019/3/1 19:19
     * @param productionOrderUpdateBatchDTO
     * @return
     * @throws XbbException
     */
    ProductionOrderUpdateBatchVO updateBatch(ProductionOrderUpdateBatchDTO productionOrderUpdateBatchDTO) throws XbbException;

    ProductionOrderUpdateBatchVO updateBatchImmediately(ProductionOrderUpdateBatchDTO productionOrderUpdateBatchDTO) throws XbbException;

    /**
     * 批量获取可退料入库的产品和数量
     * @param retMaterielListDTO
     * @return
     * @throws XbbException
     */
    RetMaterielListVO getRetMaterielMap(RetMaterielListDTO retMaterielListDTO) throws XbbException;
    /**
     * 获取可退料入库的产品和数量
     * @param productionOrder  生产单实体
     * @author lcx
     * @date 2019/2/25 11:00
     * @return Map，key为productId，value为可退料入库的数量；如果返回null，则没有可退料入库产品
     **/
    Map<Long,Double> getRetMaterielMap(ProductionOrderEntityExt productionOrder) throws XbbException;

    /**
     * 判断是否有退料入库的产品
     * @param productionOrder
     * @return true：有，false：没有
     * @author chaoxiong.lei
     */
    boolean hasReturnedMaterielInstock(ProductionOrderEntityExt productionOrder) throws XbbException;

    /**
     * 批量删除
     * @author 吴峰
     * @date 2019/3/5 21:37
     * @param productionOrderDeleteBatchDTO
     * @return com.xbongbong.pro.productionorder.pojo.vo.ProductionOrderDeleteBatchVO
     * @throws XbbException 业务异常
     */
    ProductionOrderDeleteBatchVO deleteBatch(ProductionOrderDeleteBatchDTO productionOrderDeleteBatchDTO) throws XbbException;

    /**
     * 更新生产物料出库（将生产单的关联出库单对应的id删除）
     * @param retMaterielListDTO
     * @throws XbbException
     */
    void updateReturnedMaterielInstock(InstockProductDeleteBatchVO instockProductDeleteBatchVO, RetMaterielListDTO retMaterielListDTO, Integer businessType) throws XbbException;

    /**
     * 批量编辑某个字段
     * @author 吴峰
     * @date 2019/3/20 15:22
     * @param productionOrderEditAttrUpdateDTO
     * @return void
     * @throws XbbException 业务异常
     */
    void batchEditAttr(ProductionOrderEditAttrUpdateDTO productionOrderEditAttrUpdateDTO) throws XbbException;

    /**
     * 关联新建格式化解释
     *
     * @param handlerExplainInLinkItemDTO 入参
     * @throws XbbException 异常
     */
    void formatLinkAddExplain(HandlerExplainInLinkItemDTO handlerExplainInLinkItemDTO) throws XbbException;

    /**
     * 一键生产
     * @param productionOrderAddGetDTO
     * @return
     * @throws XbbException
     */
    FormDataAddGetVO produce(ProductionOrderAddGetDTO productionOrderAddGetDTO) throws XbbException;

    /**
     * 将Excel数据格式化成表单保存需要的JSON格式的数据
     *
     * @param importFormDataDTO 导入入参
     * @param titlePojoList     标题
     * @param rowGroupPojo      一个表单对应的一组Excel填写的数据
     * @param fieldAttrList     解释
     * @return FormDataAddDTO
     * @throws XbbException XbbException
     * @author long.rao
     * @date 2019-05-06 15:03
     */
    FormDataAddDTO formatExcelValue(ImportFormDataDTO importFormDataDTO, List<CellTitlePojo> titlePojoList, RowGroupPojo rowGroupPojo, List<FieldAttrEntityForImport> fieldAttrList) throws XbbException;

    /**
     * 格式化导出数据
     *
     * @param paasFormDataESList    导出的数据
     * @param formDataListDTO       查询数据的入参
     * @param fieldAttrList         表单解释
     * @author GuoJun.HU
     * @since V1.0
     * @date 2019/5/27 10:15
     * @throws XbbException XbbException
     */
    void formatExportValue(List<PaasFormDataEntityExt> paasFormDataESList, FormDataListDTO formDataListDTO, List<FieldAttrEntityForImport> fieldAttrList) throws XbbException;

    /**
     * 修改BOM状态
     *
     * @param changeStatusDTO 入参
     * @throws XbbException
     * @author long.rao
     * @date 2019-06-06 14:31
     */
    BaseVO changeStatus(ChangeStatusDTO changeStatusDTO) throws XbbException;

    /**
     * 出库单保存后判断是否出库完毕
     * @param outstockId
     * @param refId
     * @param corpid
     * @param productSavePojoList
     * @return
     */
    boolean judgeProductionOrderAllOutBound(Long outstockId, Long refId, String corpid, List<ProductSavePojo> productSavePojoList) throws XbbException;

    /**
     * 入库单保存后判断是否出库完毕
     * @param instockId
     * @param refId
     * @param corpid
     * @param productSavePojoList
     * @return
     */
    boolean judgeProductionOrderAllInBound(Long instockId, Long refId, String corpid, List<ProductSavePojo> productSavePojoList) throws XbbException;

    /**
     * Description: 生产单 仓库一致性同步
     * @param corpid
     * @param dataId
     * @param name
     * @return void
     * @author 魏荣杰
     * @date 2019/8/5 11:22
     * @since v1.0
     */
    void dataConsistencyUpdateWarehouse(String corpid, Long dataId, String name) throws XbbException;

    /**
     * 根据业务规则，更新生产单
     * @author 吴峰
     * @date 2019/8/19 16:03
     * @param productionOrderUpdateByBusinessRuleDTO
     * @return void
     * @throws XbbException 业务异常
     */
    void updateBatchByBusinessRule(ProductionOrderUpdateByBusinessRuleDTO productionOrderUpdateByBusinessRuleDTO) throws XbbException;
    /**
     * 汇总生产单产品信息（详情页，几种产品几件）
     *
     * @param businessProductListDTO 入参
     * @throws XbbException
     * @author long.rao
     * @date 2019-08-28 10:23
     */
    ProductSummaryVO summaryProductList(BusinessProductListDTO businessProductListDTO) throws XbbException;

    void formatProduct4Show2(Long dataId, String corpid, JSONObject data, UpdateDataTypeEnum updateDataTypeEnum) throws XbbException;

    PagingProductVO getPagingProduct(PagingProductDTO pagingProductDTO) throws XbbException;

    JSONObject formatSingleProduct(ProductionOrderProductEntity entity);

    JSONObject formatSingleMaterial(ProductionOrderProductEntity entity);

    void updateBatchLabelAttr(AddLabelDTO addLabelDTO, List<PaasFormDataEntityExt> addBatchList, Boolean addOrDelete) throws XbbException;

    void afterSaveForWorkflow(SaasFormSaveDTO saasFormSaveDTO) throws XbbException;

    void afterSaveForUpdateWorkflow(SaasFormSaveDTO saasFormSaveDTO) throws XbbException;
}
